package gouhuo.allbugs.top.controller;

import gouhuo.allbugs.top.common.BaseResponse;
import gouhuo.allbugs.top.common.BusinessException;
import gouhuo.allbugs.top.common.ErrorCode;
import gouhuo.allbugs.top.model.vo.TeamUserVO;
import gouhuo.allbugs.top.model.vo.UserVO;
import gouhuo.allbugs.top.service.TeamService;
import gouhuo.allbugs.top.service.UserService;
import gouhuo.allbugs.top.service.UserTeamService;
import gouhuo.allbugs.top.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 队伍用户关系
 */
@RestController
@RequestMapping("/userTeam")
public class UserTeamController {

    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;

    @Resource
    private UserTeamService userTeamService;

    @ApiOperation("队伍成员列表")
    @GetMapping("/detail")
    public BaseResponse<List<TeamUserVO>> teamDetail(@RequestParam("teamId") String teamId) {
        if (teamId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtil.success(userTeamService.teamDetail(teamId));
    }

    @ApiOperation("获取指定用户创建的队伍")
    @GetMapping("/create")
    public BaseResponse<List<TeamUserVO>> listUserCreateTeams(@RequestParam("userId") Long userId, HttpServletRequest request) {
        return listUserTeamsByType(userId, 2, request);
    }

    @ApiOperation("获取指定用户加入的队伍")
    @GetMapping("/join")
    public BaseResponse<List<TeamUserVO>> listUserJoinTeams(@RequestParam("userId") Long userId, HttpServletRequest request) {
        return listUserTeamsByType(userId, 1, request);
    }

    @ApiOperation("根据传入的type值查询创建或加入的队伍")
    public BaseResponse<List<TeamUserVO>> listUserTeamsByType(Long userId, int type, HttpServletRequest request) {
        if (userId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getLoginUser(request, null);
        return ResultUtil.success(teamService.listTeamsByType(userVO, userId, type));
    }

}
